﻿#include "strconverthex.h"
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QTextEdit>
#include <QDebug>
StrConvertHex::StrConvertHex(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout *v_rightTop = new QVBoxLayout();
    QHBoxLayout *h_leftTop = new QHBoxLayout();
    QHBoxLayout *h_mainTop = new QHBoxLayout();

    QHBoxLayout *h_mainBottom = new QHBoxLayout();
    QVBoxLayout *v_text_input = new QVBoxLayout();
    QVBoxLayout *v_text_output = new QVBoxLayout();
    QVBoxLayout *v_main = new QVBoxLayout();

    QTextEdit *input_text = new QTextEdit(this);
    QTextEdit *output_text = new QTextEdit(this);
    output_text->setReadOnly(true);
    QLabel *input_label = new QLabel("文本字符串",this);
    QLabel *output_label = new QLabel("HEX字符串",this);

    QPushButton *btn_convert = new QPushButton("开始转换",this);
    QCheckBox *add_interval = new QCheckBox("添加\\x", this);
    add_interval->setChecked(true);
    SwitchButton * m_switchBtn = new SwitchButton(this);
    m_switchBtn->show();
    QLabel *page_tips = new QLabel("str2hex工具可以将字符换和hex之间互转, "
                                   "例如你好转换为E4BDA0E5A5BD或者 "
                                   "\\xE4\\xBD\\xA0\\xE5\\xA5\\xBD ", this);
    page_tips->setWordWrap(true);
    page_tips->setAlignment(Qt::AlignCenter);

    h_leftTop->addWidget(page_tips);
    h_leftTop->setMargin(0);
    h_leftTop->setSpacing(0);

    v_rightTop->addSpacing(5);
    v_rightTop->addWidget(m_switchBtn);
    v_rightTop->addWidget(add_interval);
    v_rightTop->addWidget(btn_convert);
    v_rightTop->setMargin(0);
    v_rightTop->setSpacing(5);

    h_mainTop->addLayout(h_leftTop);
    h_mainTop->addLayout(v_rightTop);
    h_mainTop->setStretch(0,1);
    h_mainTop->setStretch(1,0);
    h_mainTop->setMargin(0);
    h_mainTop->setContentsMargins(0, 0, 3, 0);
    h_mainTop->setSpacing(0);

    v_text_input->addWidget(input_label, 0, Qt::AlignHCenter);
    v_text_input->addWidget(input_text);
    v_text_input->setMargin(0);
    v_text_input->setSpacing(10);

    v_text_output->addWidget(output_label, 0, Qt::AlignHCenter);
    v_text_output->addWidget(output_text);
    v_text_output->setMargin(0);
    v_text_output->setSpacing(10);

    h_mainBottom->addLayout(v_text_input);
    h_mainBottom->addLayout(v_text_output);
    h_mainBottom->setContentsMargins(6, 6, 6, 0);
    h_mainBottom->setSpacing(6);

    v_main->addLayout(h_mainTop);
    v_main->addLayout(h_mainBottom);
    v_main->setMargin(0);
    v_main->setSpacing(0);

    btn_convert->setObjectName("str2hex_btn_convert");
    add_interval->setObjectName("str2hex_add_interval");
    page_tips->setObjectName("str2hex_label_tips");
    input_label->setObjectName("str2hex_label_input");
    output_label->setObjectName("str2hex_label_output");
    input_text->setObjectName("str2hex_text_input");
    output_text->setObjectName("str2hex_text_output");

    btn_convert->setProperty("class", "str2hex_widget");
    add_interval->setProperty("class", "str2hex_widget");
    page_tips->setProperty("class", "str2hex_widget");
    input_label->setProperty("class", "str2hex_widget");
    output_label->setProperty("class", "str2hex_widget");
    input_text->setProperty("class", "str2hex_widget");
    output_text->setProperty("class", "str2hex_widget");
    m_switchBtn->setProperty("class", "str2hex_widget");
    this->setLayout(v_main);
    connect(m_switchBtn, &SwitchButton::statusChanged, this, [input_label, output_label, input_text, output_text,this](bool status){
        if (!status){
            input_label->setText("文本字符串");
            output_label->setText("HEX字符串");
        }else {
            output_label->setText("文本字符串");
            input_label->setText("HEX字符串");
        }
        input_text->clear();
        output_text->clear();
    });
    connect(btn_convert, &QPushButton::clicked, this, [input_text, output_text, add_interval, m_switchBtn, this]{
        if (!input_text->toPlainText().isEmpty()){
            if (!m_switchBtn->checked()){
                if (add_interval->isChecked()){
                    QString srcData = QString("\\x"+input_text->toPlainText().toUtf8().toHex(':'));
                    output_text->setText(srcData.replace(QString(":"),QString("\\x")));
                }else {
                    output_text->setText(QString(input_text->toPlainText().toUtf8().toHex()));
                }
            }else {
                QString srcData = input_text->toPlainText();
                output_text->setText(QString(QByteArray::fromHex(srcData.replace(QString("\\x"), QString("")).toUtf8())));
            }
        }
    });
}
